﻿using Autodesk.Revit.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;

//过滤器
namespace HelloRevit
{
    [Transaction(TransactionMode.Manual)]
    public class FilterCalss : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {

                //获取当前文档.
                UIDocument uidoc = commandData.Application.ActiveUIDocument;
                
                Document doc = uidoc.Document;


                //创建收集器
                FilteredElementCollector collector = new FilteredElementCollector(doc);
                //过滤 门元素
                //1快速过滤
                //collector.OfCategory(BuiltInCategory.OST_Doors).OfClass(typeof(Wall));
                //2 通用方法  过滤墙
                ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_Walls);
                ElementClassFilter elementClassFilter = new ElementClassFilter(typeof(Wall));
                collector.WherePasses(elementClassFilter).WherePasses(elementClassFilter);

                //高亮显示实例
                var sel = uidoc.Selection.GetElementIds();

                foreach (var item in collector)
                {
                    if (item.Name == "CL_W1")
                    {
                        sel.Add(item.Id);
                    }
                }
                //高亮显示
                uidoc.Selection.SetElementIds(sel);

            }
            catch (Exception e)
            {
                message = e.Message;
                return Result.Failed;
            }

            return Result.Succeeded;
        }
        /// </ExampleMethod>
    }
}
